//
//  WH_NewDetailHeader.m
//  App_6.23
//
//  Created by wanghao on 16/7/20.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_NewDetailHeader.h"
#import "WH_Home.h"
#import "TimeModel.h"

#define LUNBOHEIGHT 300

@interface WH_NewDetailHeader ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIScrollView *bigScroll;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) TimeModel *model;

@end

@implementation WH_NewDetailHeader
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    
    return self;
    
}

- (void)createSubViews:(NSMutableArray *)images{
    
    
    if (images.count != 0){
        
        
        self.imageArray = [NSMutableArray arrayWithArray:images];
        id imageFirst = [self.imageArray firstObject];
        id imageLast = [self.imageArray lastObject];
        
        [self.imageArray insertObject:imageLast atIndex:0];
        [self.imageArray addObject:imageFirst];
        
        
        self.bigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, LUNBOHEIGHT)];
        self.bigScroll.pagingEnabled = YES;
        self.bigScroll.bounces = NO;
        self.bigScroll.showsHorizontalScrollIndicator = NO;
        self.bigScroll.delegate = self;
        self.bigScroll.contentSize = CGSizeMake(SCREEN_WIDTH*self.imageArray.count, LUNBOHEIGHT);
        self.bigScroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        //轮播图
        for (int i = 0; i < self.imageArray.count; i++) {
            
            NSString *name = self.imageArray[i];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, LUNBOHEIGHT)];
            //第三方图片赋值
            [imageView sd_setImageWithURL:[NSURL URLWithString:name] placeholderImage:nil];
            
            [self.bigScroll addSubview:imageView];
            
        }
        [self addSubview:self.bigScroll];
        //创建小点
        self.pageControl = [[UIPageControl alloc] init];
        [self addSubview:self.pageControl];
        
        [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(@(LUNBOHEIGHT));
            make.width.equalTo(@(260*WID_ONE));
            make.height.equalTo(@(20));
            
        }];
        self.pageControl.numberOfPages = [self.imageArray count] - 2;
        [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        
    }

}
-(void)createModel:(WH_Home *)model timeModel:(TimeModel *)timeModel{
    
    self.nameLabel = [UILabel new];
    _nameLabel.text = model.product_name;
    _nameLabel.font = [UIFont systemFontOfSize:14.f];
    _nameLabel.numberOfLines = 2;
    [self addSubview:_nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@5);
        make.right.mas_equalTo(@-5);
        make.top.equalTo(@(LUNBOHEIGHT + 15));
        make.height.mas_equalTo(@(40));
    }];
    
    UIView *bigView = [UIView new];
    bigView.backgroundColor = [UIColor colorWithRed:0.9 green:0.19 blue:0.29 alpha:1];
    [self addSubview:bigView];
    [bigView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.bottom.equalTo(@-10);
        
    }];
    self.periodLabel = [UILabel new];
    _periodLabel.text = [NSString stringWithFormat:@"期号: %@",model.product_period_no];
    _periodLabel.font = [UIFont systemFontOfSize:13.f];
    _periodLabel.textColor = [UIColor whiteColor];
    [bigView addSubview:_periodLabel];
    [self.periodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(@10);
        //make.height.equalTo(@20);
        make.right.equalTo(@-80);
    }];
    self.rateLabel = [UILabel new];
    //self.rateLabel.backgroundColor = [UIColor blueColor];
    [bigView addSubview:_rateLabel];
    _rateLabel.text = @"04:09:12";
    _rateLabel.textColor = [UIColor whiteColor];
    _rateLabel.font = [UIFont systemFontOfSize:23.f];
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.periodLabel);
        make.top.equalTo(self.periodLabel.mas_bottom).offset(10);
        //make.height.equalTo(@(40*HEI_ONE));
    }];

}


-(void)luckView:(WH_Home *)model{
    
    self.nameLabel = [UILabel new];
    _nameLabel.text = model.product_name;
    _nameLabel.font = [UIFont systemFontOfSize:14.f];
    _nameLabel.numberOfLines = 2;
    [self addSubview:_nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@5);
        make.right.mas_equalTo(@-5);
        make.top.equalTo(@(LUNBOHEIGHT + 15));
        make.height.mas_equalTo(@(40));
    }];

    UIView *bigViewTwo = [UIView new];
    bigViewTwo.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    [self addSubview:bigViewTwo];
    [bigViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        

        make.left.right.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom);
        make.bottom.equalTo(@-10);
        
    }];

    UIImageView *acvtorImage = [[UIImageView alloc] init];
    [acvtorImage sd_setImageWithURL:[NSURL URLWithString:model.product_image] placeholderImage:PLACEHOLDER_CELL_SAME];
    [bigViewTwo addSubview:acvtorImage];
    [acvtorImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(@10);
        make.width.height.equalTo(@40);
    }];
    
    UILabel *nameL = [UILabel new];
    nameL.text = [NSString stringWithFormat:@"获得者:  %@",model.user_nick];
    nameL.font = [UIFont systemFontOfSize:13.f];
    nameL.textColor = [UIColor lightGrayColor];
    [bigViewTwo addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(acvtorImage.mas_right).offset(10);
        make.right.equalTo(@-60);
        make.top.equalTo(@10);
        make.height.equalTo(@20);
        
    }];
    
    UILabel *IDLabel = [UILabel new];
    IDLabel.font = [UIFont systemFontOfSize:13.f];
    IDLabel.textColor = [UIColor lightGrayColor];
    
    IDLabel.text = [NSString stringWithFormat:@"用户ID:  %@(唯一不变标识)",model.user_id];
    [bigViewTwo addSubview:IDLabel];
    [IDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(acvtorImage.mas_right).offset(10);
        make.right.equalTo(@-60);
        make.top.equalTo(nameL.mas_bottom);
        make.height.equalTo(nameL);

    }];
    UILabel *periodL = [UILabel new];
    periodL.text = [NSString stringWithFormat:@"期号:  %@",model.product_period_no];
    periodL.font = [UIFont systemFontOfSize:13.f];
    periodL.textColor = [UIColor lightGrayColor];
    [bigViewTwo addSubview:periodL];
    [periodL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(acvtorImage.mas_right).offset(10);
        make.right.equalTo(@-60);
        make.top.equalTo(IDLabel.mas_bottom);
        make.height.equalTo(nameL);
    }];
    UILabel *countL = [UILabel new];
    
    countL.text = [NSString stringWithFormat:@"本期参与:  %@人次",model.user_num];
    countL.font = [UIFont systemFontOfSize:13.f];
    countL.textColor = [UIColor lightGrayColor];
    [bigViewTwo addSubview:countL];
    [countL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(acvtorImage.mas_right).offset(10);
        make.right.equalTo(@-60);
        make.top.equalTo(periodL.mas_bottom);
        make.height.equalTo(nameL);
    }];
    UILabel *kaijiangL = [UILabel new];
    kaijiangL.text = [NSString stringWithFormat:@"揭晓时间:  %@",model.kaijang_time];
    kaijiangL.font = [UIFont systemFontOfSize:13.f];
    kaijiangL.textColor = [UIColor lightGrayColor];
    [bigViewTwo addSubview:kaijiangL];
    [kaijiangL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(acvtorImage.mas_right).offset(10);
        make.right.equalTo(@-20);
        make.top.equalTo(countL.mas_bottom);
        make.height.equalTo(nameL);
    }];

    UILabel *winNumL = [UILabel new];
    winNumL.text = [NSString stringWithFormat:@"本期幸运号码:  %@",model.win_num];
    winNumL.backgroundColor = [UIColor redColor];
    winNumL.textColor = [UIColor whiteColor];
    winNumL.textAlignment = NSTextAlignmentCenter;
    [bigViewTwo addSubview:winNumL];
    [winNumL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(kaijiangL.mas_bottom).offset(5);
        make.bottom.equalTo(@-5);
        make.left.mas_equalTo(@5);
        make.right.equalTo(@-5);
    }];
}

//UIPageControl类型对象中调用的方法
- (void)changePage:(UIPageControl *)page
{
    [self.bigScroll setContentOffset:CGPointMake((page.currentPage + 1) * self.bigScroll.frame.size.width, 0) animated:YES];
}
//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.bigScroll.frame.size.width) / scrollView.frame.size.width;
    
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.bigScroll.contentOffset.x >= self.bigScroll.frame.size.width * (self.imageArray.count - 1)) {
        
        NSLog(@"最后一页");//循环滚动
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width, 0) animated:NO];
    }
    
    if (self.bigScroll.contentOffset.x <= 0) {
        
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width * (self.imageArray.count - 2), 0) animated:NO];
    }
    
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.bigScroll.contentOffset.x / self.bigScroll.frame.size.width -1;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
